package com.zmzncs.lmtc.module.promotion.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmzncs.lmtc.common.pojo.PageVO;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.promotion.pojo.duration_rule.*;
import com.zmzncs.lmtc.module.promotion.pojo.duration_rule_fee_scale.DurationRuleFeeScale;
import com.zmzncs.lmtc.module.promotion.pojo.duration_rule_fee_scale.DurationRuleFeeScaleVO;
import com.zmzncs.lmtc.module.promotion.pojo.duration_rule_park.DurationRulePark;
import com.zmzncs.lmtc.module.promotion.pojo.duration_rule_park.DurationRuleParkVO;
import com.zmzncs.lmtc.module.promotion.service.DurationRuleFeeScaleService;
import com.zmzncs.lmtc.module.promotion.service.DurationRuleParkService;
import com.zmzncs.lmtc.module.promotion.service.DurationRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 促销-时长规则 前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-08-12
 */
@Api(tags = "时长规则")
@RestController
@RequestMapping("/promotion/duration-rule")
@Validated
public class DurationRuleController {

    @Autowired
    DurationRuleService durationRuleService;
    @Autowired
    DurationRuleParkService durationRuleParkService;
    @Autowired
    DurationRuleFeeScaleService durationRuleFeeScaleService;

    @ApiOperation(value = "查询时长规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name="pageNum", value="页码", required = true),
            @ApiImplicitParam(paramType="query", name="pageSize", value="页长", required = true),
            @ApiImplicitParam(paramType="query", name="keyword", value="关键字"),
    })
    @GetMapping(value = "getDurationRulePage")
    public Rest<PageVO<DurationRuleTableVO>> getDurationRulePage(@NotNull Integer pageNum, @NotNull Integer pageSize, String keyword) {
        IPage<DurationRuleDTO> durationRulePage = durationRuleService.getDurationRuleList(new Page<>(pageNum, pageSize), keyword);
        PageVO<DurationRuleTableVO> page = JsonUtil.pageTransform(durationRulePage, DurationRuleTableVO.class);
        return RestUtil.success(page);
    }

    @ApiOperation(value = "根据id查询时长规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name="id", value="id", required = true),
    })
    @GetMapping(value = "getDurationRuleById")
    public Rest<DurationRuleInfoVO> getDurationRuleById(@NotNull Integer id) {
        //  查询时长规则
        DurationRule durationRule = durationRuleService.getById(id);
        //  根据时长规则id查询停车场
        LambdaQueryWrapper<DurationRulePark> durationRuleParkWrapper = new LambdaQueryWrapper();
        durationRuleParkWrapper.eq(DurationRulePark::getDurationRuleId, id);
        List<DurationRulePark> durationRuleParks = durationRuleParkService.list(durationRuleParkWrapper);
        List<DurationRuleParkVO> durationRuleParkVOs = JsonUtil.listTransform(durationRuleParks, DurationRuleParkVO.class);

        //  根据时长规则id查询收费标准
        LambdaQueryWrapper<DurationRuleFeeScale> durationRuleFeeScaleWrapper = new LambdaQueryWrapper();
        durationRuleFeeScaleWrapper.eq(DurationRuleFeeScale::getDurationRuleId, id);
        List<DurationRuleFeeScale> durationRuleFeeScales = durationRuleFeeScaleService.list(durationRuleFeeScaleWrapper);
        List<DurationRuleFeeScaleVO> durationRuleFeeScaleVOS = JsonUtil.listTransform(durationRuleFeeScales, DurationRuleFeeScaleVO.class);

        //  设置返回类型
        DurationRuleInfoVO durationRuleInfoVO = new DurationRuleInfoVO();
        BeanUtils.copyProperties(durationRule, durationRuleInfoVO);
        durationRuleInfoVO.setDurationRuleParks(durationRuleParkVOs);
        durationRuleInfoVO.setDurationRulemonthRentTypes(durationRuleFeeScaleVOS);

        return RestUtil.success(durationRuleInfoVO);
    }

}

